
package com.jf.cloud.multishop.controller.admin;

import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ResponseEnum;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.multishop.constant.RenovationType;
import com.jf.cloud.multishop.dto.ShopRenovationDTO;
import com.jf.cloud.multishop.model.ShopRenovation;
import com.jf.cloud.multishop.service.ShopRenovationService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 店铺装修信息
 * @author zz
 */
@RestController("adminShopRenovationController")
@RequestMapping("/mp/shop_renovation")
@Tag(name = "店铺装修信息")
public class ShopRenovationController {

    @Autowired
    private ShopRenovationService shopRenovationService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "根据类型分页获取店铺装修信息列表" , description = "分页获取店铺装修信息列表")
    public ServerResponseEntity<PageVO<ShopRenovation>> page(@Valid PageDTO pageDTO, @ParameterObject ShopRenovationDTO shopRenovation) {
        shopRenovation.setShopId(AuthUserContext.get().getTenantId());
        PageVO<ShopRenovation> shopRenovationPage = shopRenovationService.page(pageDTO, mapperFacade.map(shopRenovation, ShopRenovation.class));
        return ServerResponseEntity.success(shopRenovationPage);
    }

    @GetMapping
    @Operation(summary = "获取店铺装修信息" , description = "根据renovationId获取店铺装修信息")
    public ServerResponseEntity<ShopRenovation> getByRenovationId(@ParameterObject @RequestParam Long renovationId) {
        ShopRenovation shopRenovation = shopRenovationService.getByRenovationId(renovationId);
        if (!Objects.equals(shopRenovation.getShopId(), AuthUserContext.get().getTenantId())) {
            return ServerResponseEntity.fail(ResponseEnum.UNAUTHORIZED);
        }
        return ServerResponseEntity.success(shopRenovation);
    }

    @PostMapping("/save_pc")
    @Operation(summary = "保存店铺pc端装修信息" , description = "保存店铺装修信息")
    public ServerResponseEntity<Long> save(@Valid @RequestBody ShopRenovationDTO shopRenovationDTO) {
        ShopRenovation shopRenovation = mapperFacade.map(shopRenovationDTO, ShopRenovation.class);
        shopRenovation.setShopId(AuthUserContext.get().getTenantId());
        shopRenovation.setRenovationId(null);
        shopRenovation.setRenovationType(RenovationType.PC.value());
        shopRenovationService.save(shopRenovation);
        return ServerResponseEntity.success(shopRenovation.getRenovationId());
    }

    @PostMapping("/save_h5")
    @Operation(summary = "保存店铺移动端装修信息" , description = "保存店铺装修信息")
    public ServerResponseEntity<Long> saveH5(@Valid @RequestBody ShopRenovationDTO shopRenovationDTO) {
        ShopRenovation shopRenovation = mapperFacade.map(shopRenovationDTO, ShopRenovation.class);
        shopRenovation.setShopId(AuthUserContext.get().getTenantId());
        shopRenovation.setRenovationId(null);
        shopRenovation.setRenovationType(RenovationType.H5.value());
        shopRenovationService.save(shopRenovation);
        return ServerResponseEntity.success(shopRenovation.getRenovationId());
    }
}
